This is an R Markdown Notebook. When you execute code within the notebook, the results appear beneath the code.
Try executing this chunk by clicking the Run button within the chunk or by placing your cursor inside it and pressing Ctrl+Shift+Enter.
library(arules)
library(arulesViz)
data("Groceries")
class(Groceries)
[1] "transactions"
attr(,"package")
[1] "arules"
length(Groceries)
[1] 9835
dim(Groceries)
[1] 9835 169
itemLabels(Groceries)
[1] "frankfurter" "sausage"
[3] "liver loaf" "ham"
[5] "meat" "finished products"
[7] "organic sausage" "chicken"
[9] "turkey" "pork"
[11] "beef" "hamburger meat"
[13] "fish" "citrus fruit"
[15] "tropical fruit" "pip fruit"
[17] "grapes" "berries"
[19] "nuts/prunes" "root vegetables"
[21] "onions" "herbs"
[23] "other vegetables" "packaged fruit/vegetables"
[25] "whole milk" "butter"
[27] "curd" "dessert"
[29] "butter milk" "yogurt"
[31] "whipped/sour cream" "beverages"
[33] "UHT-milk" "condensed milk"
[35] "cream" "soft cheese"
[37] "sliced cheese" "hard cheese"
[39] "cream cheese " "processed cheese"
[41] "spread cheese" "curd cheese"
[43] "specialty cheese" "mayonnaise"
[45] "salad dressing" "tidbits"
[47] "frozen vegetables" "frozen fruits"
[49] "frozen meals" "frozen fish"
[51] "frozen chicken" "ice cream"
[53] "frozen dessert" "frozen potato products"
[55] "domestic eggs" "rolls/buns"
[57] "white bread" "brown bread"
[59] "pastry" "roll products "
[61] "semi-finished bread" "zwieback"
[63] "potato products" "flour"
[65] "salt" "rice"
[67] "pasta" "vinegar"
[69] "oil" "margarine"
[71] "specialty fat" "sugar"
[73] "artif. sweetener" "honey"
[75] "mustard" "ketchup"
[77] "spices" "soups"
[79] "ready soups" "Instant food products"
[81] "sauces" "cereals"
[83] "organic products" "baking powder"
[85] "preservation products" "pudding powder"
[87] "canned vegetables" "canned fruit"
[89] "pickled vegetables" "specialty vegetables"
[91] "jam" "sweet spreads"
[93] "meat spreads" "canned fish"
[95] "dog food" "cat food"
[97] "pet care" "baby food"
[99] "coffee" "instant coffee"
[101] "tea" "cocoa drinks"
[103] "bottled water" "soda"
[105] "misc. beverages" "fruit/vegetable juice"
[107] "syrup" "bottled beer"
[109] "canned beer" "brandy"
[111] "whisky" "liquor"
[113] "rum" "liqueur"
[115] "liquor (appetizer)" "white wine"
[117] "red/blush wine" "prosecco"
[119] "sparkling wine" "salty snack"
[121] "popcorn" "nut snack"
[123] "snack products" "long life bakery product"
[125] "waffles" "cake bar"
[127] "chewing gum" "chocolate"
[129] "cooking chocolate" "specialty chocolate"
[131] "specialty bar" "chocolate marshmallow"
[133] "candy" "seasonal products"
[135] "detergent" "softener"
[137] "decalcifier" "dish cleaner"
[139] "abrasive cleaner" "cleaner"
[141] "toilet cleaner" "bathroom cleaner"
[143] "hair spray" "dental care"
[145] "male cosmetics" "make up remover"
[147] "skin care" "female sanitary products"
[149] "baby cosmetics" "soap"
[151] "rubbing alcohol" "hygiene articles"
[153] "napkins" "dishes"
[155] "cookware" "kitchen utensil"
[157] "cling film/bags" "kitchen towels"
[159] "house keeping products" "candles"
[161] "light bulbs" "sound storage medium"
[163] "newspapers" "photo/film"
[165] "pot plants" "flower soil/fertilizer"
[167] "flower (seeds)" "shopping bags"
[169] "bags"
summary(Groceries)
transactions as itemMatrix in sparse format with
9835 rows (elements/itemsets/transactions) and
169 columns (items) and a density of 0.02609146
most frequent items:
whole milk other vegetables rolls/buns soda
2513 1903 1809 1715
yogurt (Other)
1372 34055
element (itemset/transaction) length distribution:
sizes
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
2159 1643 1299 1005 855 645 545 438 350 246 182 117 78 77 55 46
17 18 19 20 21 22 23 24 26 27 28 29 32
29 14 14 9 11 4 6 1 1 1 1 3 1
Min. 1st Qu. Median Mean 3rd Qu. Max.
1.000 2.000 3.000 4.409 6.000 32.000
includes extended item information - examples:
image(Groceries)
itemFrequencyPlot(Groceries,topN=10,cex.names=1)
rules <- apriori(Groceries,parameter = list(supp=0.01,conf=0.5,maxlen=10,target="rules"))
Apriori
Parameter specification:
Algorithmic control:
Absolute minimum support count: 98
set item appearances ...[0 item(s)] done [0.00s].
set transactions ...[169 item(s), 9835 transaction(s)] done [0.00s].
sorting and recoding items ... [88 item(s)] done [0.00s].
creating transaction tree ... done [0.00s].
checking subsets of size 1 2 3 4 done [0.00s].
writing ... [15 rule(s)] done [0.00s].
creating S4 object ... done [0.00s].
summary(rules)
set of 15 rules
rule length distribution (lhs + rhs):sizes
3
15
Min. 1st Qu. Median Mean 3rd Qu. Max.
3 3 3 3 3 3
summary of quality measures:
support confidence coverage lift
Min. :0.01007 Min. :0.5000 Min. :0.01729 Min. :1.984
1st Qu.:0.01174 1st Qu.:0.5151 1st Qu.:0.02089 1st Qu.:2.036
Median :0.01230 Median :0.5245 Median :0.02430 Median :2.203
Mean :0.01316 Mean :0.5411 Mean :0.02454 Mean :2.299
3rd Qu.:0.01403 3rd Qu.:0.5718 3rd Qu.:0.02598 3rd Qu.:2.432
Max. :0.02227 Max. :0.5862 Max. :0.04342 Max. :3.030
count
Min. : 99.0
1st Qu.:115.5
Median :121.0
Mean :129.4
3rd Qu.:138.0
Max. :219.0
mining info:
inspect(rules)
soda_rules_rhs <- apriori(Groceries,parameter=list(supp=0.01,conf=0.5,maxlen=10,minlen=2),
appearance=list(default="lhs",rhs="soda"))
Apriori
Parameter specification:
Algorithmic control:
Absolute minimum support count: 98
set item appearances ...[1 item(s)] done [0.00s].
set transactions ...[169 item(s), 9835 transaction(s)] done [0.00s].
sorting and recoding items ... [88 item(s)] done [0.00s].
creating transaction tree ... done [0.00s].
checking subsets of size 1 2 3 4 done [0.00s].
writing ... [0 rule(s)] done [0.00s].
creating S4 object ... done [0.00s].
inspect(soda_rules_rhs)
soda_rules_lhs <- apriori(Groceries,parameter=list(supp=0.01,conf=0.5,maxlen=10,minlen=2),
appearance=list(default="rhs",lhs="soda"))
Apriori
Parameter specification:
Algorithmic control:
Absolute minimum support count: 98
set item appearances ...[1 item(s)] done [0.00s].
set transactions ...[169 item(s), 9835 transaction(s)] done [0.00s].
sorting and recoding items ... [88 item(s)] done [0.00s].
creating transaction tree ... done [0.00s].
checking subsets of size 1 2 done [0.00s].
writing ... [0 rule(s)] done [0.00s].
creating S4 object ... done [0.00s].
inspect(soda_rules_lhs)
plot(rules)
plot(rules,engine="plotly")
subrules <- head(rules,n=10,by="confidence")
plot(subrules,method="graph",engine="htmlwidget")
plot(subrules,method="paracoord")
Add a new chunk by clicking the Insert Chunk button on the toolbar or by pressing Ctrl+Alt+I.
When you save the notebook, an HTML file containing the code and output will be saved alongside it (click the Preview button or press Ctrl+Shift+K to preview the HTML file).
The preview shows you a rendered HTML copy of the contents of the editor. Consequently, unlike Knit, Preview does not run any R code chunks. Instead, the output of the chunk when it was last run in the editor is displayed.